package CCNQ::Proxy::inbound;
# Copyright (C) 2006, 2007  Stephane Alnet
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# 

#
# For more information visit http://carrierclass.net/
#
use strict; use warnings;

use base qw(CCNQ::Proxy::Base);

sub doc
{
    return <<'HTML';

    Inbound trunks. List the IP addresses of gateways or providers which
    are "trusted" (=not authenticated) and treated as PSTN calls.
    <p>
    You must also assign a Type which indicates what kind of
    manipulations should be done on the SIP headers to interoperate
    with the target.
    <p>
    Make sure to include Voicemail (VM) and Telephone User Interface
    (TUI) servers as well so that Message Waiting Indication (MWI) and
    call redirection can work properly.
    <p>
    The Source parameter must be an IP:port combination.

HTML
}


use CCNQ::Proxy::outbound;

sub gw_type_names
{
    return CCNQ::Proxy::outbound::gw_type_names;
}

sub form
{
    my $self = shift;
    return (
        'Source' => 'text',
        'Type'   => $self->gw_type_names,
    );
}

sub insert
{
    my $self = shift;
    my %params = @_;
    my $source  = $params{source};
    my $type    = $params{type};

    return ()
        unless defined $source and defined $type;
    
    die "Source must be an IP:port"
        unless $source =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,5}$/;
    my $ip_src = $1;

    return (
        <<'SQL',[$ip_src],
            INSERT INTO trusted(src_ip,proto,from_pattern) VALUES (?,'any','^sip:.*$')
SQL
        $self->_avp_set($source,'src_gw_type',$type),
    );
}

sub delete
{
    my $self = shift;
    my %params = @_;
    my $source = $params{source};

    warn "Source must be an IP:port"
        unless $source =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):\d{1,5}$/;
    my $ip_src = $1;

    return (
        <<'SQL',[$ip_src],
            DELETE FROM trusted WHERE src_ip = ?
SQL
        $self->_avp_set($source,'src_gw_type',undef),
    );
}

sub list
{
    my $self = shift;
    our %type_names = @{$self->gw_type_names};

    return (
        <<'SQL',
            SELECT uuid AS Source, value AS Type
            FROM avpops
            WHERE attribute = ?
            ORDER BY uuid ASC
SQL
        [$self->avp->{src_gw_type}],
        sub {
            my ($content_orig,$names_orig) = @_;
            my $content = [(@{$content_orig})];
            my $names   = [(@{$names_orig})];
            $names->[1] = 'Type*'; # hide
            $names->[2] = 'Type!'; # no value
            $content->[2] = $type_names{$content->[1]};
            return ($content,$names);
        }
    );
}

1;